package com.koalac.ironman.widget;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.foot.travel.R;

/**
 * 
 * @description 加载相关的对话框，此部分作为外部调用即可。
 *              <p>
 *              此部分作为单纯的对话框也可以，可以指定当前的是否需要出现确定、取消
 *              </p>
 *              <p>
 *              增加了进度条相关的数据处理
 *              </p>
 * @author chenxiaohua
 * @Date 2014-6-9
 */
public class LoadingDialog extends Dialog {

	private ProgressBar progressBar;
	private TextView progressText;
	private LinearLayout contentView;

	protected LoadingDialog(Context context) {
		super(context);
		this.initCustomLayout();
	}

	protected LoadingDialog(Context context, int theme) {
		super(context, theme);
		this.initCustomLayout();
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	public static LoadingDialog createLoadingDialog(Context context,
			CharSequence title, CharSequence message) {
		return createLoadingDialog(context, title, message, false);
	}
	
	public static LoadingDialog createUpLoadLoadingDialog(Context context,
			CharSequence title, CharSequence message) {
		return createLoadingDialog(context, title, message, false);
	}

	public static LoadingDialog createLoadingDialog(Context context,
			CharSequence title, CharSequence message, boolean indeterminate) {
		return createLoadingDialog(context, title, message, indeterminate,
				true, null);
	}

	public static LoadingDialog createLoadingDialog(Context context,
			CharSequence title, CharSequence message, boolean indeterminate,
			boolean cancelable) {
		return createLoadingDialog(context, title, message, indeterminate,
				cancelable, null);
	}

	/**
	 * 
	 * @return
	 * @description 创建当前的加载对话框
	* @author chenxiaohua
 * @Date 2014-6-9
	 */
	public static LoadingDialog createLoadingDialog(Context context,
			CharSequence title, CharSequence message, boolean indeterminate,
			boolean cancelable, OnCancelListener cancelListener) {
		LoadingDialog dialog = new LoadingDialog(context,
				R.style.LoadingDialogTheme);
		dialog.setCancelable(cancelable);
		dialog.setOnCancelListener(cancelListener);
		dialog.progressText.setText(message);
		// android.view.WindowManager.LayoutParams p = dialog.getWindow()
		// .getAttributes();
		// if (p != null) {
		// /**
		// * 输入字体不能太宽，否则有问题; 先如此，暂时不去计算
		// */
		// p.width = AmsClientSystemInfo.width / 2;
		// dialog.getWindow().setAttributes(p);
		// }
		return dialog;
	}

	/**
	 * 
	 * @param strText
	 * @description 设置当前的对话框的消息
	* @author chenxiaohua
 * @Date 2014-6-9
	 */
	public final void setDialogMessageText(String strText) {
		this.progressText.setText(strText);
	}

	/**
	 * 
	 * @return
	 * @description 获取当前显示的Dialog的文本
	 * @Date 2013 2013-11-27
	 * @author chenzf
	 */
	public final String getDialogMessage() {
		return this.progressText.getText().toString();
	}

	/**
	 * 
	 * @param progress
	 *            0~100数值
	 * @description 设置当前的进度
	 * @author chenxiaohua
 * @Date 2014-6-9
	 */
	public final void setProgress(int progress) {
		this.progressBar.setProgress(progress);
	}
	

	@Override
	public boolean onKeyUp(int keyCode, KeyEvent event) {
//		if (keyCode == KeyEvent.KEYCODE_BACK && event.isTracking()
//				&& !event.isCanceled() && LoadingDialog.this.mCancelable) {
//			this.cancel();
//			return true;
//		}
		return super.onKeyUp(keyCode, event);
	}

	/**
	 * 
	 * @description 初始化客户端的布局
	* @author chenxiaohua
 * @Date 2014-6-9
	 */
	private void initCustomLayout() {
		contentView = (LinearLayout) LayoutInflater.from(this.getContext())
				.inflate(R.layout.common_loadingdialog, null);
		progressBar = (ProgressBar) contentView
				.findViewById(R.id.com_LoadingProgressBar);
		progressText = (TextView) contentView
				.findViewById(R.id.com_LoadingProgressText);
		progressBar.setProgress(0);
		setContentView(contentView);
	}
}
